package com.zcx.qshop.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.activity.AppPictureFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilBase64;
import com.zcx.helper.util.UtilSDCard;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.action.LoginAction;
import com.zcx.qshop.activity.AddressManageActivity;
import com.zcx.qshop.activity.BalanceActivity;
import com.zcx.qshop.activity.CollectionActivity;
import com.zcx.qshop.activity.CouponActivity;
import com.zcx.qshop.activity.EditNameActivity;
import com.zcx.qshop.activity.HistoryActivity;
import com.zcx.qshop.activity.LoginActivity;
import com.zcx.qshop.activity.MyOrderActivity;
import com.zcx.qshop.activity.OpinionActivity;
import com.zcx.qshop.activity.SettingActivity;
import com.zcx.qshop.conn.JsonAvatarAsyPost;
import com.zcx.qshop.conn.JsonUserAsyGet;
import com.zcx.qshop.dialog.PicurlDialog;

/**
 * Created by Administrator on 2015/8/8 0008.
 */
public class MyFragment extends AppPictureFragment implements View.OnClickListener{

    @BoundView(R.id.my_is_login) private View isLogin;

    @BoundView(R.id.my_login) private ImageView login;

    @BoundView(R.id.my_head) private SimpleDraweeView head;

    @BoundView(R.id.my_edit) private ImageView edit;

    @BoundView(R.id.my_name) private TextView name;

    @BoundView(R.id.my_recharge) private View recharge;

    @BoundView(R.id.my_order) private View order;

    @BoundView(R.id.my_collection) private View collection;

    @BoundView(R.id.my_coupon) private View coupon;

    @BoundView(R.id.my_address) private View address;

    @BoundView(R.id.my_history) private View history;

    @BoundView(R.id.my_setting) private View setting;

    @BoundView(R.id.my_opinion) private View opinion;

    private LoginAction.OnLoginStateListener onLoginStateListener = new LoginAction.OnLoginStateListener() {

        @Override
        public void onLogin(JsonUserAsyGet.Info info) {

            try{

                login.setVisibility(View.GONE);

                isLogin.setVisibility(View.VISIBLE);

                name.setText(info.name);

                head.setImageURI(Uri.parse(info.picurl));

            }catch (Exception e){}

        }

        @Override
        public void onExit() {

            login.setVisibility(View.VISIBLE);

            isLogin.setVisibility(View.GONE);

        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_my, null)));

        login.setOnClickListener(this);

        head.setOnClickListener(this);

        edit.setOnClickListener(this);

        order.setOnClickListener(this);

        recharge.setOnClickListener(this);

        collection.setOnClickListener(this);

        coupon.setOnClickListener(this);

        address.setOnClickListener(this);

        history.setOnClickListener(this);

        setting.setOnClickListener(this);

        opinion.setOnClickListener(this);

        QSApplication.LoginAction.addOnLoginStateListener(onLoginStateListener);

        return view;

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.my_login:

                startVerifyActivity(LoginActivity.class);

                break;

            case R.id.my_head:

                new PicurlDialog(getActivity()){

                    @Override
                    protected void onCamera() {

                        startCamera(null);

                    }

                    @Override
                    protected void onAlbum() {

                        startAlbum(null);

                    }

                }.show();

                break;

            case R.id.my_recharge:

                startVerifyActivity(BalanceActivity.class);

                break;

            case R.id.my_edit:

                startVerifyActivity(EditNameActivity.class);

                break;

            case R.id.my_order:

                startVerifyActivity(MyOrderActivity.class);

                break;

            case R.id.my_collection:

                startVerifyActivity(CollectionActivity.class);

                break;

            case R.id.my_coupon:

                startVerifyActivity(CouponActivity.class);

                break;

            case R.id.my_address:

                startVerifyActivity(AddressManageActivity.class);

                break;

            case R.id.my_history:

                startVerifyActivity(HistoryActivity.class);

                break;

            case R.id.my_setting:

                startVerifyActivity(SettingActivity.class);

                break;

            case R.id.my_opinion:

                startVerifyActivity(OpinionActivity.class);

                break;

        }

    }

    @Override
    protected Crop getCrop() {

        return new Crop().setCrop(true);

    }

    @Override
    protected String getCameraAbsolutePath() {

        return UtilSDCard.getSDCardPath() + "yc/image/";

    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {

        try {

            new JsonAvatarAsyPost(QSApplication.QSPreferences.readUid(), UtilBase64.encodeBase64File(s), new AsyCallBack<JsonAvatarAsyPost.Info>(){

                @Override
                public void onSuccess(String toast, int type, JsonAvatarAsyPost.Info info) throws Exception {

                    QSApplication.LoginAction.setPicurl(info.picurl);

                }

            }).execute(getActivity());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    @Override
    public void startVerifyActivity(Class<?> cls, Intent intent) {

        intent.setClass(getActivity(), cls);

        if(!QSApplication.LoginAction.isLogin() && (cls.equals(MyOrderActivity.class) || cls.equals(CollectionActivity.class) || cls.equals(CouponActivity.class) || cls.equals(AddressManageActivity.class) || cls.equals(HistoryActivity.class) || cls.equals(OpinionActivity.class) || cls.equals(BalanceActivity.class))){

            intent.putExtra("Class", cls);

            intent.setClass(getActivity(), LoginActivity.class);

        }

        startActivity(intent);

    }

    @Override
    public void onDestroy() {

        QSApplication.LoginAction.removeOnLoginStateListener(onLoginStateListener);

        super.onDestroy();

    }

}
